package com.leyou.controller;

import com.github.pagehelper.PageInfo;
import com.leyou.ResponseData;
import com.leyou.entity.Brand;
import com.leyou.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    // 分页查询品牌信息
    @GetMapping("/page")
    public ResponseData<PageInfo<Brand>> queryBrandPageInfo(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortColumn", required = false) String sortColumn,
            @RequestParam(value = "isDesc", defaultValue = "false") boolean isDesc,
            @RequestParam(value = "key", required = false) String key) {

        try {
            PageInfo<Brand> pageInfo = brandService.selectBrandPageInfo(pageNum, pageSize, sortColumn, isDesc, key);
            ResponseData<PageInfo<Brand>> rd = new ResponseData<>();
            rd.setData(pageInfo);
            return rd;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
        }
        return ResponseData.error(5000, "服务器繁忙", null);
    }

    @PostMapping
    public ResponseData saveBrand(Brand brand, List<Long> cids) {
        brandService.saveBrand(brand,cids);
        return ResponseData.ok();
    }
}
